package com.ksm.zyz.controller;

import javax.servlet.http.HttpServletRequest;

import com.ksm.zyz.common.base.BaseController;
import com.ksm.zyz.common.base.Global;
import com.ksm.zyz.common.util.DateUtil;
import com.ksm.zyz.sys.entity.ExtData;
import com.ksm.zyz.sys.entity.Message;
import com.ksm.zyz.sys.entity.SysDept;
import com.ksm.zyz.sys.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author litao
 *
 */
@Controller
@RequestMapping("dept")
public class SysDeptController extends BaseController<SysDept> {

	@Autowired // 自动set
	private SysDeptService sysDeptService;

	/**
	 * 查询全部
	 *
	 * @param sysDept
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public ExtData selectAll(SysDept sysDept, HttpServletRequest req) {
		sysDept.setBsData(getBsData(req));
		extData.setList(sysDeptService.selectAll(sysDept));
		extData.setTotalCount(sysDeptService.sysselectCount(sysDept));
		return extData;
	}

	/**
	 * 单位新增
	 * 
	 * @param sysDept
	 * @return
	 */
	@RequestMapping(value = "create",method = RequestMethod.POST)
	@ResponseBody
	public Message create(SysDept sysDept) {
		sysDept.setId(DateUtil.getShortUuid());
		sysDeptService.save(sysDept);
		return new Message(Global.SUCCESSFUL);
	}

	/**
	 * 单位修改
	 * 
	 * @param sysDept
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public Message update(SysDept sysDept) {
		sysDeptService.update(sysDept);
		return new Message(Global.SUCCESSFUL);
	}

	/**
	 * 单位删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Message delete(String ids) {
		String[] deptids = ids.split(",");
		sysDeptService.delete(deptids);
		return new Message(Global.SUCCESSFUL);
	}

	/**
	 * 单位启用或停用
	 * 
	 * @param ids
	 * 0停用，1启用
	 * @return
	 */
	@RequestMapping(value = "stop")
	@ResponseBody
	public Message stop(String ids) {
		String[] deptids = ids.split(",");
		map.put("deptids", deptids);
		map.put("state", "0");
		sysDeptService.change(map);
		return new Message(Global.SUCCESSFUL);
	}

	@RequestMapping(value = "start")
	@ResponseBody
	public Message start(String ids) {
		String[] deptids = ids.split(",");
		map.put("deptids", deptids);
		map.put("state", "1");
		sysDeptService.change(map);
		return new Message(Global.SUCCESSFUL);
	}

}
